package chapter5.Application_Events;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericXmlApplicationContext;

/**
 * 
 * @author chauthanhthang
 * @comment example using application event when call method publish(String
 *          message) will run onApplicationEvent() in class MessageEventListener
 */
public class Publisher implements ApplicationContextAware {
	private ApplicationContext ctx;

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.ctx = arg0;
	}

	public void publish(String message) {
		ctx.publishEvent(new MessageEvent(this, message));
	}

	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:chapter5/Application_Events/Application_Events.xml");
		ctx.refresh();

		Publisher publisher = (Publisher) ctx.getBean("publisher");
		publisher.publish("Hello world!");
		publisher.publish("This is example with application event");
	}
}
